package com.haowu.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haowu.common.core.utils.StringUtils;
import com.haowu.common.mybatis.core.page.PageQuery;
import com.haowu.common.mybatis.core.page.TableDataInfo;
import com.haowu.product.domain.HwService;
import com.haowu.product.domain.bo.HwServiceBo;
import com.haowu.product.domain.vo.HwServiceVo;
import com.haowu.product.mapper.HwAppServiceMapper;
import com.haowu.product.service.IHwAppServiceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 好务服务项目Service业务层处理
 *
 * @author lky
 * @date 2023-02-23
 */
@RequiredArgsConstructor
@Service
public class HwAppServiceServiceImpl implements IHwAppServiceService {

    private final HwAppServiceMapper baseMapper;

    /**
     * 查询好务服务项目
     */
    @Override
    public HwServiceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询好务服务项目列表
     */
    @Override
    public TableDataInfo<HwServiceVo> queryPageList(HwServiceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HwService> lqw = buildQueryWrapper(bo);
        Page<HwServiceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询好务服务项目列表
     */
    @Override
    public List<HwServiceVo> queryList(HwServiceBo bo) {
        LambdaQueryWrapper<HwService> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HwService> buildQueryWrapper(HwServiceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HwService> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, HwService::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getCityId() != null, HwService::getCityId, bo.getCityId());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceCode()), HwService::getServiceCode, bo.getServiceCode());
        lqw.like(StringUtils.isNotBlank(bo.getServiceName()), HwService::getServiceName, bo.getServiceName());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceStatus()), HwService::getServiceStatus, bo.getServiceStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRecommended()), HwService::getRecommended, bo.getRecommended());
        lqw.eq(StringUtils.isNotBlank(bo.getIsEvent()), HwService::getIsEvent, bo.getIsEvent());
        lqw.eq(bo.getIsDelete() != null, HwService::getIsDelete, bo.getIsDelete());
        lqw.eq(bo.getServiceNumber() != null, HwService::getServiceNumber, bo.getServiceNumber());
        lqw.eq(bo.getPraiseRate() != null, HwService::getPraiseRate, bo.getPraiseRate());
        lqw.eq(bo.getSort() != null, HwService::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增好务服务项目
     */
    @Override
    public Boolean insertByBo(HwServiceBo bo) {
        HwService add = BeanUtil.toBean(bo, HwService.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改好务服务项目
     */
    @Override
    public Boolean updateByBo(HwServiceBo bo) {
        HwService update = BeanUtil.toBean(bo, HwService.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HwService entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除好务服务项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
